package com.news.servlet;



import com.alibaba.fastjson.JSON;
import com.news.pojo.News;
import com.news.pojo.Page;
import com.news.service.NewsSerice;
import com.news.service.impl.NewsSericeImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/news.do")
public class NewsServlet extends HttpServlet {
private NewsSerice newsSerice = new NewsSericeImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost( req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //获取请求
            request.setCharacterEncoding("UTF-8");
            //设置响应
            response.setContentType("text/html;charset=utf-8");
            //控制请求路由
            String opr = request.getParameter("opr");
            switch (opr){
                case "add":{
                    add(request,response);
                    break;
                }
                case "del":{
                    del(request,response);
                    break;
                }
                case "getAll":{
                    System.out.println("getAll方法");
                    getAll(request,response);
                    break;
                }
                case "getNewsByPage":{
                    System.out.println("page方法");
                    getNewsByPage(request,response);
                    break;
                }
                default:{
                    response.sendRedirect("/404.jsp");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            response.sendRedirect("/500.jsp");
        }

    }
    //增加新闻
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //servlet处理页面
        System.out.println("add新闻");
        News news = new News();
        int ntid = Integer.valueOf(request.getParameter("ntid"));
        news.setNtid(ntid);
        news.setNauthor(request.getParameter("nauthor"));
        news.setNcontent(request.getParameter("ncontent"));
        news.setNcreateDate(request.getParameter("ncreatedate"));
        news.setNpicPath(request.getParameter("npicpath"));
        news.setNmodifyDate(request.getParameter("nmodifydate"));
        news.setNsummary(request.getParameter("nsummary"));
        news.setNtitle(request.getParameter("ntitle"));
        //响应
        HttpSession session = request.getSession();
        session.setAttribute("news_info",newsSerice.addNews(news));
        response.sendRedirect("admin/news_add.jsp");

    }
    //删除新闻
    public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
       String param = request.getParameter("nid");
       int nid = Integer.valueOf(param);
       request.getSession().setAttribute("news_del",newsSerice.delNews(nid));
       response.sendRedirect("admin/news_del.jsp");
    }
    //查看所有新闻
    public void getAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(newsSerice.getAllNews()));
        //out.print(newsSerice.getAllNews());
        out.flush();
        out.close();
    }

    public void getNewsByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        int currPageNo = request.getParameter("currPageNo") == null?1:Integer.parseInt(request.getParameter("currPageNo"));
        int pageSize = request.getParameter("pageSize") == null?15:Integer.parseInt(request.getParameter("pageSize"));
        System.out.println(currPageNo);
        Page<News> page = new Page<News>();
        page.setCurrPageNo(currPageNo);
        page.setPageSize(pageSize);
        News news = new News();
        int ntid = request.getParameter("ntid") == null?0:Integer.parseInt(request.getParameter("ntid"));
        news.setNid(ntid);
        //响应
        response.setContentType("application/json;charset=utf-8");
        System.out.println(news.getNid());
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(newsSerice.getNewsByPage(page,news)));
        out.flush();
        out.close();
    }
}
